package com.frost.demo2;

import java.util.Arrays;

public class TestDemo {

    // 只出现一次的数字
    public static int func3(int[] arr) {
        int tmp = 0;
        for (int i = 0; i < arr.length; i++) {
            tmp ^= arr[i];
        }
        return tmp;
    }

    public static void main(String[] args) {
        int[] arr = {1,70,36,6,85,19,5,3,1,70,36,6,85,19,5};
        int ret = func3(arr);
        System.out.println(ret);
    }

    // 冒泡
    public static void bubble(int[] arr) {
        boolean flg = false;
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j] > arr[j+1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                    flg = true;
                }
            }
            if (flg == false) {
                break;
            }
        }
    }

    public static void main2(String[] args) {
        int[] arr = {1,70,36,6,85,19,5,3};
        bubble(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static int func(int[] arr,int key) {
        int left = 0;
        int right = arr.length - 1;

        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] < key) {
                left = mid + 1;
            } else if (arr[mid] == key) {
                return mid;
            }else {
                right = mid - 1;
            }
        }

        return -1;
    }

    public static boolean func1(int[] arr) {
        boolean flg = false;
        for (int i = 0; i < arr.length-1; i++) {
            if (arr[i] > arr[i+1]) {
                flg = true;
            }
        }

        return flg;
    }

    public static void main1(String[] args) {
        int[] arr1 = {1,2,3,4,5,6,7,8,9,10};
        boolean flg = func1(arr1);
        if (flg) {
            System.out.println("无序");
        }else {
            System.out.println("有序");
        }
    }
}
